package interact;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import processing.core.PVector;

import run.Doodler;
import run.Env;
import run.FigureCell;
import run.LineCell;
import run.Doodler.CellMode;

public class MouseHander {
	
	
	  // Hold it down; attract cells toward mouse pointer
	  
	  // TODO indirect mouse cursor influence; not quite exactly,
	  // with some drag and smoothing of the motion.
	  
	  // HANDLE DRAGS; e.g. if you gesture with the mouse in a curve, 
	  // let the new cell start with some inflection to trace the trajectory you made

	private PVector start;
	private PVector end; 
	public MouseHander(final Doodler p, final Env env) {
		p.addMouseListener(new MouseListener() {

			public void mouseClicked(MouseEvent e) {
				CellMode mode = Doodler.instance.getCellMode();
				if (mode.equals(CellMode.Figure)) {
					System.out.println("add figurepoint");
					env.addFigurePoint(new PVector(e.getX(), e.getY(), 0));
				}
				

			}

			public void mouseEntered(MouseEvent e) {
				
			}

			public void mouseExited(MouseEvent e) {
				
			}

			// TODO when click, start a new cell
			// let the cell follow the mouse and 'desire' to be where
			// the mouse is, with Spring Force
			// until you let go
			
			public void mousePressed(MouseEvent e) {
				start = new PVector(e.getX(), e.getY(), 0);
			}

			public void mouseReleased(MouseEvent e) {
				CellMode mode = Doodler.instance.getCellMode();
				end = new PVector(e.getX(), e.getY(), 0);
				
				PVector dir = PVector.sub(end, start);
				if (end.dist(start) < 5) {
					dir = new  PVector(p.random(-1,1), p.random(-1,1), 0);
				}

				if (mode.equals(CellMode.Line)) {
					env.addCell(new LineCell(start, dir, 0));
				}
				if (mode.equals(CellMode.FigureLine)) {
					env.addCell(new FigureCell(start, dir, 0));
				}

			}});
	}

}
